package com.kuqi.presenters;

import android.content.Context;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

import java.util.logging.Handler;

// 这个类的实际意义在于监听Activity的生命那个周期
// 在生命周期的不同阶段调用Presenter类不同的处理方式
// 主要目的是为了缩小Activity中的代码量，方便维护
public abstract class BasePresenter implements LifecycleObserver {

     // 上下文
     private Context context;
     // 当前的生命周期阶段
     private Lifecycle lifecycle;

     public BasePresenter(Context context, Lifecycle lifecycle) {
          this.context = context;
          this.lifecycle = lifecycle;
          lifecycle.addObserver(this);
     }

     @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
     void onCreate() {
          create();
     }

     @OnLifecycleEvent(Lifecycle.Event.ON_START)
     void onStart() {
          start();
     }

     @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
     void onDestroy() {
          destroy();
     }

     @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
     void onResume() {
          resume();
     }

     @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
     void onPause() {
          pause();
     }

     @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
     void onStop() {
          stop();
     }

//---------------- 各个阶段调用各种方法， 可用extends并override让其自己调用-----------
     protected  void create(){};

     protected  void start(){};

     protected  void destroy(){};

     protected  void resume(){};

     protected  void pause(){};

     protected  void stop(){};
//----------------------------------------------------------------------------------

     public Context getContext() {
          return context;
     }

     public Lifecycle getLifecycle() {
          return lifecycle;
     }
}
